<script setup></script>

<template>
  <!-- 登陆界面 -->
  <div class="body">
    <div class="login-wrap">
      <el-form class="login-container" :model="form" :rules="rules" ref="formRef">
        <h1 class="title">用户登录</h1>
        <el-form-item label="" prop="username">
          <el-input type="text" v-model="form.username" placeholder="登录账号"></el-input>
        </el-form-item>
        <el-form-item label="" prop="password">
          <el-input type="password" v-model="form.password" placeholder="登录密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="doSubmit()">登陆</el-button>
        </el-form-item>
        <el-row style="text-align: center; margin-top: -10px">
          <!-- <el-col :span="12"><el-link type="primary">忘记密码</el-link></el-col> -->
          <el-col :span="24"><el-link type="primary" @click="gotoRegister()">用户注册</el-link></el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
.body {
  width: 100%;
  height: 100%;
  /* 背景颜色 */
  background-image: linear-gradient(to bottom, #ffffff 0%, #9edaf6 100%);
}

.login-wrap {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 90vh;
}

.login-container {
  border-radius: 10px;
  margin: 0px auto;
  width: 350px;
  padding: 30px 35px 15px 35px;
  background: #fff;
  border: 1px solid #eaeaea;
  text-align: left;
  box-shadow: 0 0 20px 2px rgba(0, 0, 0, 0.1);
}

.title {
  margin: 0px auto 40px auto;
  text-align: center;
  color: #505458;
}
</style>
<script>
export default {
  name: "Register",
  data() {
    return {
      form: {
        username: "wu",
        password: "11"
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ]
      }
    }
  },
  methods: {
    gotoRegister(){
      this.$router.push('/register')
    },
    doSubmit() {
      this.$refs['formRef'].validate((valid) => {
        // console.log(valid);
        if (valid) {
          this.$request.post('/login', this.form).then(res => {
            if (res.code == '200') {
              this.$message.success('登录成功')
              //保存登陆token
              // console.log(res);
              sessionStorage.setItem('token', res.data.token)
              sessionStorage.setItem('user', res.data.user)
              //跳转首页
              let isBelong = JSON.parse(res.data.user)?.isBelong
              if(isBelong == 0){
                this.$router.push('/home/subject')
              }else{
                this.$router.push('/home/select')
              }
            } else {
              this.$message.error(res.msg)
            }
          }).catch(err => {
            this.$message.error('登录异常' + err)
          })
        }
      })
    }
  }
}



</script>